#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;
#include <unordered_map>
class Solution {
public:
    string evaluate(string s, vector<vector<string>>& knowledge) {
        string ans;
        unordered_map<string, string> hash;
        for (int i = 0; i < knowledge.size(); i++)
        {
            hash[knowledge[i][0]] = knowledge[i][1];
        }

        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '(')
            {
                int j = i;
                while (s[j] != ')')
                {
                    j++;
                }
                string tmp(s.begin() + i + 1, s.begin() + j);
                if (hash.count(tmp))
                    ans.append(hash[tmp]);
                else
                    ans.append("?");
                i = j;
            }
            else
            {
                ans.push_back(s[i]);
            }
        }
        return ans;
    }
};